package com.nmf.pc.company.controller;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.nmf.pc.vo.MemberRegisterReq;
import com.nmf.pc.vo.ReturnResult;
import com.nmf.site.biz.member.gen.entity.BizMember;
import com.nmf.site.biz.member.gen.service.BizMemberService;
import com.nmf.site.biz.member.genx.emums.MemberLevel;
import com.nmf.site.biz.member.genx.service.GxBizMemberService;
import com.nmf.site.biz.sms.bx.service.SmsCodeService;
import com.nmf.site.common.excep.KideException;
import com.nmf.site.common.excep.KideExpCode;
import com.nmf.site.common.utils.IdGenerater;
import com.nmf.site.modules.sys.service.SystemService;

@Controller
@RequestMapping(value = "/pc/p")
public class PcCompanyRegisterController {
	@Autowired
	BizMemberService bizMemberService;
	@Autowired
	SmsCodeService smsCodeService;
	@Autowired
	IdGenerater idGenerater;
	@Autowired
	GxBizMemberService gxBizMemberService;
	@RequestMapping(value = {"mem/register"},method = {RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public ReturnResult register(@Valid MemberRegisterReq memberRegisterReq, BindingResult result,HttpServletRequest request, HttpServletResponse response) {
		ReturnResult returnResult = new ReturnResult();
		
		
		BizMember bizMember = new BizMember();
		bizMember.setTelNo(memberRegisterReq.getTelNo());
		List<BizMember> blist = bizMemberService.findList(bizMember);
		
		if(blist.size()==1) {
			throw new KideException(KideExpCode.MEMBER_EXIST, memberRegisterReq.getTelNo()+"");
		}else {
			MemberLevel memberLevel = MemberLevel.getStatus(memberRegisterReq.getGender());
			bizMember.setGender(memberLevel.value);
			bizMember.setLevel(MemberLevel.NOMEBER.value);
			bizMember.setMemberName(memberRegisterReq.getMemberName());
			bizMember.setGender(memberRegisterReq.getGender());
			bizMember.setInDate(new Date());
			if(StringUtils.isNoneBlank(memberRegisterReq.getInvitationCode())) {
				BizMember dbBizMember = gxBizMemberService.getByInvcode(memberRegisterReq.getInvitationCode());
				
				if(dbBizMember==null) {
					throw new KideException(KideExpCode.MEMBER_INVCODE_NOTEXIST, "");
				}
			}
			bizMember.setInvitationCode(memberRegisterReq.getInvitationCode());
			bizMember.setSelfInvitationCode(idGenerater.genInvNo());
			String enPassword = SystemService.entryptPassword(memberRegisterReq.getPassword());
			bizMember.setPassword(enPassword);
			bizMemberService.save(bizMember);
			returnResult.setMessage("注册成功");
		}
		return returnResult;
	}
}
